package com.smrtcold.dev.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Pattern;

/**
 * Copyright (C) SmartCold 版权所有
 *
 * @author MaQiang34
 * @Description: 基类
 * @createDate:2018-12-07 19:43
 * @version:V1.0
 */
public class Utils {

    public static Pattern PATTERN = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
    public final static byte[] AES_KEY = {1, -112, 54, 114, 56, 94, -71, 81, -63, 87, 65, 77, 27, -14, 43, 54};

    public static final Gson GSON = new Gson();
    public static final DecimalFormat NF = new DecimalFormat("#");
    public static final DecimalFormat DF = new DecimalFormat("##0.00");
    public static final DecimalFormat D4 = new DecimalFormat("##0.0000");


    public static final Type MAPTYPE = new TypeToken<Map<String, Object>>() {
    }.getType();
    public static final Type LISTMAPTYPE = new TypeToken<List<Map<String, Object>>>() {
    }.getType();

    public static boolean isNull(final String str) {
        return str == null || str.isEmpty();
    }

    public static boolean isnotNull(final String str) {
        return str != null && !str.isEmpty();
    }

    public static boolean isNull(Collection list) {
        return list == null || list.isEmpty();
    }

    public static boolean isnotNull(Collection list) {
        return list != null && !list.isEmpty();
    }

    public static boolean isNull(Map map) {
        return map == null || map.isEmpty();
    }

    public static boolean isnotNull(Map map) {
        return map != null && !map.isEmpty();
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    //网页token

    public static Boolean isNumer(String str) {
        return null != str && PATTERN.matcher(str).matches();
    }

    public static double getDouble(double value) {
        try {
            return Double.parseDouble(Utils.DF.format(value));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0.0;
    }

    public static double getDouble4(double value) {
        return Double.parseDouble(Utils.D4.format(value));
    }



    public static Boolean verWebToken(String token) {
        return Utils.verAck().equals(token);
    }


    public static Boolean verAck(String token) {
        return Utils.isnotNull(token) && token.length() > 5 && Integer.parseInt(token.substring(4)) == (Integer.parseInt(token.substring(0, 4)) * 33 - 11) * 22;
    }
    public static String verAck() {
        return Integer.toString((Calendar.getInstance().get(Calendar.DAY_OF_MONTH) * 33 - 11) * 22);
    }

    public static Boolean verAck(String token, String phone) {
        try {
            if (isNull(token) || isNull(phone) || token.length() < 5 || phone.length() < 5 || !phone.substring(phone.length() - 1).equals(token.substring(3, 4))) {
                return false;
            }
            return verAck(token);
        } catch (Exception e) {
        }
        return false;
    }


    public static void main(String[] args) {
        System.err.println(verAck("29852166868"));
    }
    //
    public static String getCode() {
        String cuttime = Long.toString(System.currentTimeMillis());
        return cuttime.substring(cuttime.length() - 4);
    }

    public static Integer[] getId(String termstr) {
        if (null == termstr || "".equals(termstr.trim())) {
            return new Integer[]{};
        }
        String[] split = termstr.split(",");
        Integer[] v = new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            v[i] = Integer.parseInt(split[i]);
        }
        return v;
    }

    public static Long[] getIds(String termstr) {
        if (null == termstr || "".equals(termstr.trim())) {
            return new Long[]{};
        }
        String[] split = termstr.split(",");
        Long[] v = new Long[split.length];
        for (int i = 0; i < split.length; i++) {
            v[i] = Long.parseLong(split[i]);
        }
        return v;
    }

    public static String JOIN(String[] objs, String f) {
        StringBuffer buffer = new StringBuffer();
        for (String o : objs) {
            if (null != o && o.length() < 8) {
                buffer.append(String.format(f, o));
            }
        }
        return buffer.delete(buffer.length() - 1, buffer.length()).toString();
    }


    public static String getIdS(Object[] i) {
        if (i != null && i.length > 0) {
            String str = Arrays.toString(i);
            return str.substring(1, str.length() - 1);
        }
        return null;
    }


}
